package com.wl.shixun1.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fraction {
    private int f1, f2; // f1是分子, f2是分母


    private int gongyue(int a, int b) { // 求最大公约
        int m, n, r;
        if (a > b) {
            m = a;
            n = b;
        } else {
            m = b;
            n = a;
        }
        while (n != 0) {
            r = m % n;
            m = n;
            n = r;
        }
        return m;
    }

    public Fraction add(Fraction x) {
        Fraction result = new Fraction();
        result.f1 = f1 * x.f2 + x.f1 * f2;
        result.f2 = f2 * x.f2;
        int t = gongyue(result.f1, result.f2);
        result.f1 /= t;
        result.f2 /= t;
        return result;
    }

    public String drawl() {
        String s;
        if (f2 == 1) {
            s = f1 + "";
        } else if (f1 * f2 > 0) {
            s = (f1 + "/" + f2);
        } else {
            s = ("-" + Math.abs(f1) + "/" + Math.abs(f2));
        }
        return s;
    }
}